/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "arm_math.h"
#include "uart_print.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static void DSP_Negate(void)
{
  float32_t pSrc = 0.0f;
  float32_t pDst;
  q31_t pSrc1 = 0;
  q31_t pDst1;
  q15_t pSrc2 = 0;
  q15_t pDst2;
  q7_t pSrc3 = 0;
  q7_t pDst3;
  /*求相反数*********************************/
  pSrc -= 1.23f;
  arm_negate_f32(&pSrc, &pDst, 1);
  uart_print("arm_negate_f32 = %f\r\n", pDst);
  pSrc1 -= 1;
  arm_negate_q31(&pSrc1, &pDst1, 1);
  uart_print("arm_negate_q31 = %d\r\n", pDst1);
  pSrc2 -= 1;
  arm_negate_q15(&pSrc2, &pDst2, 1);
  uart_print("arm_negate_q15 = %d\r\n", pDst2);
  pSrc3 += 1;
  arm_negate_q7(&pSrc3, &pDst3, 1);
  uart_print("arm_negate_q7 = %d\r\n", pDst3);
  uart_print("***********************************\r\n");
}

static void DSP_Offset(void)
{
  float32_t pSrcA = 0.0f;
  float32_t Offset = 0.0f;
  float32_t pDst;
  q31_t pSrcA1 = 0;
  q31_t Offset1 = 0;
  q31_t pDst1;
  q15_t pSrcA2 = 0;
  q15_t Offset2 = 0;
  q15_t pDst2;
  q7_t pSrcA3 = 0;
  q7_t Offset3 = 0;q7_t pDst3;
  /*求偏移*********************************/
  Offset--;
  arm_offset_f32(&pSrcA, Offset, &pDst, 1);
  uart_print("arm_offset_f32 = %f\r\n", pDst);
  Offset1--;
  arm_offset_q31(&pSrcA1, Offset1, &pDst1, 1);
  uart_print("arm_offset_q31 = %d\r\n", pDst1);
  Offset2--;
  arm_offset_q15(&pSrcA2, Offset2, &pDst2, 1);
  uart_print("arm_offset_q15 = %d\r\n", pDst2);
  Offset3--;
  arm_offset_q7(&pSrcA3, Offset3, &pDst3, 1);
  uart_print("arm_offset_q7 = %d\r\n", pDst3);
  uart_print("***********************************\r\n");
}

static void DSP_Shift(void)
{
  q31_t pSrcA1 = 0x88886666;
  q31_t pDst1;
  q15_t pSrcA2 = 0x8866;
  q15_t pDst2;
  q7_t pSrcA3 = 0x86;
  q7_t pDst3;
  /*求移位*********************************/
  arm_shift_q31(&pSrcA1, 3, &pDst1, 1);
  uart_print("arm_shift_q31 = %8x\r\n", pDst1);
  arm_shift_q15(&pSrcA2, -3, &pDst2, 1);
  uart_print("arm_shift_q15 = %4x\r\n", pDst2);
  arm_shift_q7(&pSrcA3, 3, &pDst3, 1);
  uart_print("arm_shift_q7 = %2x\r\n", pDst3);
  uart_print("***********************************\r\n");
}

static void DSP_Sub(void)
{
  float32_t pSrcA[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t pSrcB[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t pDst[5];
  q31_t pSrcA1[5] = {1,1,1,1,1};
  q31_t pSrcB1[5] = {1,1,1,1,1};
  q31_t pDst1[5];
  q15_t pSrcA2[5] = {1,1,1,1,1};
  q15_t pSrcB2[5] = {1,1,1,1,1};
  q15_t pDst2[5];
  q7_t pSrcA3[5] = {0x70,1,1,1,1};
  q7_t pSrcB3[5] = {0x7f,1,1,1,1};
  q7_t pDst3[5];
  /*求减法*********************************/
  pSrcA[0] += 1.1f;
  arm_sub_f32(pSrcA, pSrcB, pDst, 5);
  uart_print("arm_sub_f32 = %f\r\n", pDst[0]);
  pSrcA1[0] += 1;
  arm_sub_q31(pSrcA1, pSrcB1, pDst1, 5);
  uart_print("arm_sub_q31 = %d\r\n", pDst1[0]);
  pSrcA2[0] += 1;
  arm_sub_q15(pSrcA2, pSrcB2, pDst2, 5);
  uart_print("arm_sub_q15 = %d\r\n", pDst2[0]);
  pSrcA3[0] += 1;
  arm_sub_q7(pSrcA3, pSrcB3, pDst3, 5);
  uart_print("arm_sub_q7 = %d\r\n", pDst3[0]);
  uart_print("***********************************\r\n");
}

static void DSP_Scale(void)
{
  float32_t pSrcA[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t scale = 0.0f;
  float32_t pDst[5];
  q31_t pSrcA1[5] = {0x6fffffff,1,1,1,1};
  q31_t scale1 = 0x6fffffff;
  q31_t pDst1[5];
  q15_t pSrcA2[5] = {0x6fff,1,1,1,1};
  q15_t scale2 = 0x6fff;
  q15_t pDst2[5];
  q7_t pSrcA3[5] = {0x70,1,1,1,1};
  q7_t scale3 = 0x6f;
  q7_t pDst3[5];
  /*求比例因子计算*********************************/
  scale += 0.1f;
  arm_scale_f32(pSrcA, scale, pDst, 5);
  uart_print("arm_scale_f32 = %f\r\n", pDst[0]);
  scale1 += 1;
  arm_scale_q31(pSrcA1, scale1, 0, pDst1, 5);
  uart_print("arm_scale_q31 = %x\r\n", pDst1[0]);
  scale2 += 1;
  arm_scale_q15(pSrcA2, scale2, 0, pDst2, 5);
  uart_print("arm_scale_q15 = %x\r\n", pDst2[0]);
  scale3 += 1;
  arm_scale_q7(pSrcA3, scale3, 0, pDst3, 5);
  uart_print("arm_scale_q7 = %x\r\n", pDst3[0]);
  uart_print("***********************************\r\n");
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* Enable I-Cache---------------------------------------------------------*/
  SCB_EnableICache();

  /* Enable D-Cache---------------------------------------------------------*/
  SCB_EnableDCache();

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  DSP_Negate();
  DSP_Offset();
  DSP_Shift();
  DSP_Sub();
  DSP_Scale();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Activate the Over-Drive mode
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
